package com.crm.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.crm.dao.CommentsDao;
import com.crm.entity.Comments;
import com.crm.service.CommentsService;

@Service
public class CommentsServiceImpl implements CommentsService {

	@Autowired
	private CommentsDao commentsDao;

	@Override
	public Page<Comments> findConmmentsByTopicId(String topicId, Integer pageIndex) {
		Comments comments = new Comments();
		comments.setTopicId(topicId);
		//创建实例
        Example<Comments> example = Example.of(comments);
        PageRequest pageable = new PageRequest(pageIndex,10,Sort.Direction.DESC,"addTime");
		return commentsDao.findAll(example, pageable);
	}

	@Override
	public void addComments(Comments comments) {
		if(StringUtils.isBlank(comments.getUserName())) {
			comments.setUserName("匿名用户");
		}
		comments.setAddTime(new Date());
		commentsDao.save(comments);
	}

	@Override
	public List<Comments> getConmments(String topicId) {
		return commentsDao.findByTopicId(topicId);
	}

	@Override
	public Page<Comments> getConmmentsByPage(Integer pageIndex, String topicId) {
		Comments comments = new Comments();
		if(StringUtils.isNotBlank(topicId)) {
			comments.setTopicId(topicId);
		}
		//创建实例
        Example<Comments> example = Example.of(comments);
        PageRequest pageable = new PageRequest(pageIndex,10,Sort.Direction.DESC,"addTime");
		return commentsDao.findAll(example, pageable);
	}

}
